using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UwinEducation.API.Attributes;
using UwinEducation.Application.Common.Interfaces;
using UwinEducation.Application.Dtos;
using UwinEducation.Shared.Models;

namespace UwinEducation.API.Controllers
{
    /// <summary>
    /// 预约管理控制器
    /// </summary>
    [Authorize]
    //[RequirePermission("student")] // 指定所需的权限
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class AppointmentController : ControllerBase
    {
        private readonly IAppointmentService _appointmentService;

        public AppointmentController(IAppointmentService appointmentService)
        {
            _appointmentService = appointmentService;
        }

        /// <summary>
        /// 获取预约分页列表
        /// </summary>
        [HttpPost]
        public async Task<ApiResponse<PagedResult<AppointmentDto>>> GetPagedList([FromBody] AppointmentPageRequest request)
        {
            var result = await _appointmentService.GetPagedListAsync(request);
            return ApiResponse<PagedResult<AppointmentDto>>.Ok(result);
        }

        /// <summary>
        /// 获取预约详情
        /// </summary>
        [HttpPost]
        public async Task<ApiResponse<AppointmentDto>> GetById([FromBody] IdRequest request)
        {
            var result = await _appointmentService.GetByIdAsync(request.Id);
            return ApiResponse<AppointmentDto>.Ok(result);
        }

        /// <summary>
        /// 创建预约
        /// </summary>
        [HttpPost]
        [RequirePermission("student")] // 指定所需的权限
        public async Task<ApiResponse<AppointmentDto>> Create([FromBody] CreateAppointmentDto input)
        {
            var result = await _appointmentService.CreateAsync(input);
            return ApiResponse<AppointmentDto>.Ok(result);
        }

        /// <summary>
        /// 更新预约
        /// </summary>
        [HttpPost]
        [RequirePermission("student")] // 指定所需的权限
        public async Task<ApiResponse<AppointmentDto>> Update([FromBody] UpdateAppointmentDto input)
        {
            var result = await _appointmentService.UpdateAsync(input);
            return ApiResponse<AppointmentDto>.Ok(result);
        }

        /// <summary>
        /// 删除预约
        /// </summary>
        [HttpPost]
        [RequirePermission("student")] // 指定所需的权限
        public async Task<ApiResponse<object>> Delete([FromBody] IdRequest request)
        {
            await _appointmentService.DeleteAsync(request.Id);
            return ApiResponse<object>.Ok(null);
        }

        /// <summary>
        /// 获取学生的预约列表
        /// </summary>
        [HttpPost]
        public async Task<ApiResponse<List<AppointmentDto>>> GetByStudentId([FromBody] IdRequest request)
        {
            var result = await _appointmentService.GetByStudentIdAsync(request.Id);
            return ApiResponse<List<AppointmentDto>>.Ok(result);
        }

        /// <summary>
        /// 获取教师的预约列表
        /// </summary>
        [HttpPost]
        public async Task<ApiResponse<List<AppointmentDto>>> GetByTeacherId([FromBody] IdRequest request)
        {
            var result = await _appointmentService.GetByTeacherIdAsync(request.Id);
            return ApiResponse<List<AppointmentDto>>.Ok(result);
        }
    }
} 